package cn.njyd.corejava.thread.myday04;


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

public class CountDownLatchDemo01 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(40);
        for (int i=1;i<=40;i++) {
            new Thread(()->{
                int time = ThreadLocalRandom.current().nextInt(1000);
                try{
                    TimeUnit.MICROSECONDS.sleep(time);
                    System.out.println(Thread.currentThread().getName()+"离开了教室");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    countDownLatch.countDown();
                }
            },"第"+i+"个同学").start();
        }
        countDownLatch.await();
        System.out.println("全部离开教室，锁门");
    }

}
